﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Domain.HU;
using HiStaff.iSystem;
using Janus.Windows.GridEX;

namespace HiStaff.Control
{
    public partial class HiEmployeeSelection : UserControl
    {
        private bool _readOnlyWhenEdit = false;
        private Size _popupSize = new Size(800, 600);
        private bool _searchWithText = false;
        private HiEmployeeFilter employeeFilter = new HiEmployeeFilter();

        public event HiEmployeeSelectionChanged SelectedEmployeeChanged;

        public List<GridEXColumn> ExtraColumns
        {
            get { return employeeFilter.ExtraColumns; }
            set { employeeFilter.ExtraColumns = value; }
        }
        [DefaultValue(false)]
        public bool IsRequire
        {
            get
            {
                return mcbEmployee.IsRequire;
            }
            set
            {
                mcbEmployee.IsRequire = value;
            }
        }
        public string DataFieldID { get; set; }
        public string DataFieldCode { get; set; }
        public string DataFieldFirstName { get; set; }
        public string DataFieldLastName { get; set; }
        public string DataFieldOrgID { get; set; }
        public string DataFieldOrganization { get; set; }
        public string DataFieldPosID { get; set; }
        public string DataFieldPosition { get; set; }
        public string DataFieldInsCenterID { get; set; }
        public string DataFieldInsCenterName { get; set; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public decimal? SelectedID
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).ID;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { ID = value });
                    EmployeeSelected = lst;
                }
                else
                {

                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].ID = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedCode
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).CODE;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { CODE = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].CODE = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedFirstName
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).FIRSTNAME_VN;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { FIRSTNAME_VN = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].FIRSTNAME_VN = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedLastName
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).LASTNAME_VN;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { LASTNAME_VN = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].LASTNAME_VN = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public decimal? SelectedOrgID
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).ORGID;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { ORGID = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].ORGID = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public decimal? SelectedPosID
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).POSID;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { POSID = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].POSID = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedOrganization
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).ORGNAME;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { ORGNAME = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].ORGNAME = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedPosition
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).POSNAME;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { POSNAME = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].POSNAME = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedInsCenterID
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).ORGINS;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { ORGINS = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].ORGINS = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedInsCenterName
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                    return ((EMPLOYEE_SELECTION)mcbEmployee.SelectedItem).ORGINSNAME;
                else return null;
            }
            set
            {
                if (mcbEmployee.DropDownList.RowCount != 1)
                {
                    var lst = new List<EMPLOYEE_SELECTION>();
                    lst.Add(new EMPLOYEE_SELECTION { ORGINSNAME = value });
                    EmployeeSelected = lst;
                }
                else
                {
                    ((List<EMPLOYEE_SELECTION>)mcbEmployee.DataSource)[0].ORGINSNAME = value;
                }
                mcbEmployee.Refresh();
                mcbEmployee.SelectedIndex = 0;
            }
        }

        [DefaultValue(true)]
        public bool VisibleColumnsOrganization
        {
            get { return mcbEmployee.DropDownList.Columns["ORGNAME"].Visible; }
            set { mcbEmployee.DropDownList.Columns["ORGNAME"].Visible = value; }
        }
        [DefaultValue(true)]
        public bool VisibleColumnsPosition
        {
            get { return mcbEmployee.DropDownList.Columns["POSNAME"].Visible; }
            set { mcbEmployee.DropDownList.Columns["POSNAME"].Visible = value; }
        }
        [DefaultValue(false)]
        public bool VisibleColumnsInsuranceCenter
        {
            get { return mcbEmployee.DropDownList.Columns["ORGINSNAME"].Visible; }
            set { mcbEmployee.DropDownList.Columns["ORGINSNAME"].Visible = value; }
        }
        [DefaultValue(false)]
        public bool ReadOnly
        {
            get
            {
                return !btnSelect.Enabled;
            }
            set
            {
                btnSelect.Enabled = !value;
                mcbEmployee.ComboStyle = value ? Janus.Windows.GridEX.ComboStyle.DropDownList : Janus.Windows.GridEX.ComboStyle.Combo;
            }
        }
        public bool ReadOnlyWhenEdit
        {
            get { return _readOnlyWhenEdit; }
            set { _readOnlyWhenEdit = value; }
        }
        [DefaultValue(typeof(Size), "800,600")]
        public Size PopupSize { get { return _popupSize; } set { _popupSize = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<EMPLOYEE_SELECTION> EmployeeSelected
        {
            get
            {
                return employeeFilter.EmployeeSelected;
            }
            set
            {
                if (value != null && value.Count > 0)
                {
                    mcbEmployee.DataSource = null;
                    mcbEmployee.SetDataBinding(value, "ID");
                    employeeFilter.EmployeeSelected = value;
                    mcbEmployee.SelectedIndex = 0;
                }
                else
                {
                    employeeFilter.EmployeeSelected = new List<EMPLOYEE_SELECTION>();
                    mcbEmployee.DataSource = null;
                    mcbEmployee.SelectedIndex = -1;
                }
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public EMPLOYEE_SELECTION SelectedItem
        {
            get
            {
                if (mcbEmployee.SelectedItem != null)
                {
                    return (EMPLOYEE_SELECTION)mcbEmployee.SelectedItem;
                }
                else return null;
            }
        }
        public bool SelectMultiEmployee
        {
            get { return employeeFilter.FilterMultiEmployee; }
            set { employeeFilter.FilterMultiEmployee = value; }
        }

        public HiEmployeeSelection()
        {
            InitializeComponent();
            this.mcbEmployee.ComboStyle = Janus.Windows.GridEX.ComboStyle.Combo;
        }
        private void HiEmployeeSelection_Load(object sender, EventArgs e)
        {
            try
            {
                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    if (employeeFilter.EmployeeSelected == null) EmployeeSelected = new List<EMPLOYEE_SELECTION>();

                    mcbEmployee.ValueMember = "ID";
                    mcbEmployee.DisplayMember = "NAMEWITHCODE";

                    if (ExtraColumns != null)
                    {
                        foreach (var col in ExtraColumns)
                        {
                            mcbEmployee.DropDownList.RootTable.Columns.Add(col);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                mcbEmployee.Text = ex.Message;
            }
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                _searchWithText = false;

                this.Parent.Cursor = Cursors.WaitCursor;
                Form frm = new Form();
                frm.Size = _popupSize;
                frm.Icon = Properties.Resources.histafff;
                frm.StartPosition = FormStartPosition.CenterParent;
                frm.Text = "Chọn nhân viên";
                frm.ShowInTaskbar = false;
                frm.Controls.Add(employeeFilter);
                employeeFilter.Dock = DockStyle.Fill;
                frm.ShowDialog();
                if (employeeFilter.EmployeeSelected.Count > 0)
                {
                    mcbEmployee.DataSource = null;
                    mcbEmployee.SetDataBinding(employeeFilter.EmployeeSelected, "ID");
                    mcbEmployee.SelectedIndex = 0;
                }
                else
                {
                    mcbEmployee.SelectedIndex = -1;
                    mcbEmployee.DataSource = null;
                }
            }
            catch (Exception ex)
            {
                mcbEmployee.Text = ex.Message;
            }
            finally
            {
                this.Parent.Cursor = Cursors.Default;
            }
        }

        private void mcbEmployee_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                if (e.KeyChar == (char)Keys.Return)
                {
                    _searchWithText = false;
                    if (string.IsNullOrEmpty(mcbEmployee.Text.Trim()))
                    {
                        mcbEmployee.DataSource = null;
                        mcbEmployee.SelectedIndex = -1;
                    }
                    else
                    {
                        var lstSelection = Service.ProfileClient.GetEmployeeSelection(new EMPLOYEE_SEARCH
                        {
                            ORGID = 1,
                            CODE = mcbEmployee.Text
                        });
                        if (lstSelection.Count == 1)
                        {
                            mcbEmployee.DataSource = null;
                            mcbEmployee.SetDataBinding(lstSelection, "ID");
                            employeeFilter.EmployeeSelected = lstSelection;
                            mcbEmployee.SelectedIndex = 0;
                        }
                        else if (lstSelection.Count > 1)
                        {
                            _searchWithText = true;

                            mcbEmployee.DataSource = null;
                            mcbEmployee.SetDataBinding(lstSelection, "ID");
                            mcbEmployee.DroppedDown = true;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                mcbEmployee.Text = ex.Message;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void mcbEmployee_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    if (_searchWithText)
                    {
                        if (mcbEmployee.SelectedItem != null)
                        {
                            var item = (EMPLOYEE_SELECTION)mcbEmployee.SelectedItem;

                            var data = new List<EMPLOYEE_SELECTION>();
                            data.Add(item);
                            mcbEmployee.DataSource = null;
                            mcbEmployee.SetDataBinding(data, "ID");
                            employeeFilter.EmployeeSelected = data;
                            mcbEmployee.SelectedIndex = 0;
                            _searchWithText = false;
                        }
                    }

                    if (SelectedEmployeeChanged != null)
                        SelectedEmployeeChanged(this, e);
                }
            }
            catch (Exception ex)
            {
                mcbEmployee.Text = ex.Message;
            }
        }

        private void mcbEmployee_Leave(object sender, EventArgs e)
        {
            try
            {
                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    if (mcbEmployee.DropDownList.RowCount > 0 && mcbEmployee.SelectedItem == null)
                    {
                        mcbEmployee.DataSource = null;
                    }
                }
            }
            catch (Exception ex)
            {
                mcbEmployee.Text = ex.Message;
            }
        }
    }
}
